<?php

namespace app\index\service\wechatprocess;
use app\common\model\jsm\Keywords as KeywordsModel;
use app\common\model\jsm\Members as MembersModel;
use app\common\model\jsm\Messages as MessagesModel;
use app\common\jsmApi\User;

/**
 * 微信扫码service
 */
class WechatMsgProcessorService extends BaseProcessorService{

    private $_openid  = null;
    private $_appid   = null;
    private $_companyId = null;
    private $_mpId    = null;
    private $_xmlArr  = null;
    private $_addtime = null;
    private $_message = null;
    private $_isKeyword = 0;
    
    
    /**
     * 构造函数
     * @param type $mps
     * @param type $message
     */
    public function __construct($mps,$message) {
        
        $this->_message = $message;
        $this->_xmlArr = $message['XmlArr'];
        
        $this->_openid = $message['openid'];
        $this->_appid = $message['app_id'];
        $this->_addtime = $message['add_time'];
        $this->_companyId = $mps['o_id'];
        $this->_mpId = $mps['mp_id'];
    }
    
    /**
     * 处理消息的主进程
     */
    public function DealMsg(){
        
        $membersModel = new MembersModel($this->_mpId);
        $members = $membersModel->getMembersByCondition(['openid' => $this->_openid]);
        
        //依据消息类型不同做不同逻辑处理
        switch (true){
            //图片类型， 将图片通过jsm接口拉取到本地并返回地址
            case $this->_message['msg_type'] == 'image':
                return $this->_imageMsg($members);
            //文本类型， 判断文本是否关键字
            case $this->_message['msg_type'] == 'text':
                return $this->_textMsg($members);
        }
        
        return false;
    }




    /**
     * 图片消息
     */
    private function _imageMsg($members){
        $this->_xmlArr['Content'] = '';
        $wechatUser = new User();
        $wechatUser->setParams(['authorizer_appid' => $this->_appid]);
        $rdata = $wechatUser->uploadPic([
            'mediaId' => $this->_xmlArr['MediaId'],
            'msgtype' => $this->_xmlArr['MsgType']
        ]);
        if ($rdata->code == 0) {
            $this->_xmlArr['Content'] = $rdata->data;
        }
        
        $result = $this->_saveWeChatMessage($members);
        if($result){
            return true;
        }
        
        return ['msg'=>'image msg fail'];
    }
    
    /**
     * 文本消息
     */
    private function _textMsg($members){
        $keyWordsModel = new KeywordsModel();
        $keyWords = $keyWordsModel->getKeywordsByCondition(['mp_id'=>$this->_mpId,'command' => $this->_xmlArr['Content']]);
        if (!empty($keyWords)) {
            $this->_isKeyword = 1;
        }
        $result = $this->_saveWeChatMessage($members);
        if($result){
           return true;
        }
        
        return ['msg'=>'image text fail'];
    }
    
    /**
     * 保存微信消息
     * @param type $members
     */
    private function _saveWeChatMessage($members){
        $idata = [
            'openid'     => $this->_openid,
            'nickname'   => $members['nickname'],
            'headimgurl' => $members['headimgurl'],
            'msg_id'     => $this->_xmlArr['MsgId'],
            'msgtype'    => $this->_xmlArr['MsgType'],
            'content'    => $this->_xmlArr['Content'],
            'is_keyword' => $this->_isKeyword,
            'create_ts'  => $this->_addtime,
        ];
        $messagesModel = new MessagesModel($this->_mpId);
        return $messagesModel->addMessages($idata);
    }
}
